package com.niit.dao;

import com.niit.pojo.StudentTable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Keven-赵仕星
 * @date 2022/2/11 10:31
 */
public class DisplayIndividual {
    Configuration conf;

    public DisplayIndividual() {
        HbaseConnection hbaseConnection = new HbaseConnection();
        Configuration conf = hbaseConnection.connect();
        this.conf = conf;
    }

    public List<StudentTable> display(StudentTable studentTable) throws IOException {
        HTable hTable = new HTable(conf, "Student_Table");
        System.out.println("111" + studentTable.getStudentStatus());
        Scan scan = new Scan();
        ResultScanner rs = hTable.getScanner(scan);
        List<StudentTable> list = new ArrayList<>();

        for (Result result : rs) {
            String studentID = Bytes.toString(result.getRow());
            String name = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("Name")));
            String dateOfEnrollment = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("DateOfEnrollment")));
            String contactNumber = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("ContactNumber")));
            String semester = Bytes.toString(result.getValue(Bytes.toBytes("status"), Bytes.toBytes("Semester")));
            String studentStatus = Bytes.toString(result.getValue(Bytes.toBytes("status"), Bytes.toBytes("StudentStatus")));
            Boolean b = true;
            if (!studentTable.getStudentId().isEmpty()) {
                if (!studentID.equals(studentTable.getStudentId())) b = false;
            }
            if (!studentTable.getName().isEmpty() && (b == true)) {
                if (!name.equals(studentTable.getName())) b = false;
            }
            if (!studentTable.getDateOfEnrollment().isEmpty() && (b == true)) {
                if (!dateOfEnrollment.equals(studentTable.getDateOfEnrollment())) b = false;
            }
            if (!studentTable.getContactNumber().isEmpty() && (b == true)) {
                if (!contactNumber.equals(studentTable.getContactNumber())) b = false;
            }
            if (!studentTable.getSemester().isEmpty() && (b == true)) {
                if (!semester.equals(studentTable.getSemester())) b = false;
            }
            if (!studentTable.getStudentStatus().isEmpty() && (b == true)) {
                if (!studentStatus.equals(studentTable.getStudentStatus())) b = false;
            }
            if (b) {
                StudentTable x = new StudentTable();
                x.setStudentId(studentID);
                x.setName(name);
                x.setDateOfEnrollment(dateOfEnrollment);
                x.setContactNumber(contactNumber);
                x.setSemester(semester);
                x.setStudentStatus(studentStatus);
                System.out.println(x);
                list.add(x);
            }
        }
        rs.close();
        return list;
    }
}
